Skip to content

Deduplicate raw MCP text envelope construction with shared helper#4352

Merged
lpcox merged 2 commits intomainfrom
copilot/fix-duplicate-mcp-text-envelope
Apr 22, 2026
Merged

Deduplicate raw MCP text envelope construction with shared helper#4352
lpcox merged 2 commits intomainfrom
copilot/fix-duplicate-mcp-text-envelope

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 22, 2026

Raw MCP text-content envelopes ({"content":[{"type":"text","text":"..."}]}) were being hand-constructed in multiple production paths, creating drift risk if the envelope shape changes. This PR centralizes that shape behind one helper and updates existing call sites to use it.

  • Shared MCP response helper

    • Added mcp.BuildMCPTextResponse(text string) map[string]interface{} in internal/mcp/tool_result.go.
    • Establishes a single source of truth for raw text envelope shape used by guard-facing/raw-map flows.
  • Call site consolidation

    • Replaced inline envelope maps in:
      • internal/server/unified.go (callCollaboratorPermission)
      • internal/proxy/proxy.go (restBackendCaller)
      • internal/server/system_tools.go (sysInit)
      • internal/server/system_tools.go (listServers)
  • Targeted unit coverage

    • Added TestBuildMCPTextResponse in internal/mcp/tool_result_test.go to validate envelope structure and text passthrough.
func BuildMCPTextResponse(text string) map[string]interface{} {
	return map[string]interface{}{
		"content": []map[string]interface{}{
			{"type": "text", "text": text},
		},
	}
}

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • example.com
    • Triggering command: /tmp/go-build3674478355/b509/launcher.test /tmp/go-build3674478355/b509/launcher.test -test.testlogfile=/tmp/go-build3674478355/b509/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true rotocol/go-sdk@v1.5.0/internal/json/json.go -I x_amd64/vet --gdwarf-5 --64 ut-4038412272.c x_amd64/vet -I g_.a -fPIC x_amd64/vet -pthread go-sdk/oauthex -fmessage-length-bool x_amd64/vet (dns block)
    • Triggering command: /tmp/go-build2111533505/b513/launcher.test /tmp/go-build2111533505/b513/launcher.test -test.testlogfile=/tmp/go-build2111533505/b513/testlog.txt -test.paniconexit0 -test.timeout=10m0s (dns block)
  • invalid-host-that-does-not-exist-12345.com
    • Triggering command: /tmp/go-build3674478355/b491/config.test /tmp/go-build3674478355/b491/config.test -test.testlogfile=/tmp/go-build3674478355/b491/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true /internal/httpcommon/ascii.go /internal/httpcommon/headermap.go x_amd64/vet --gdwarf-5 ternal/engine/in-atomic -o x_amd64/vet 2897�� g_.a -I x_amd64/vet --gdwarf-5 (dns block)
    • Triggering command: /tmp/go-build2111533505/b495/config.test /tmp/go-build2111533505/b495/config.test -test.testlogfile=/tmp/go-build2111533505/b495/testlog.txt -test.paniconexit0 -test.timeout=10m0s 2897�� g_.a -goversion 64/pkg/tool/linux_amd64/vet -c=4 -nolocalimports -importcfg 64/pkg/tool/linux_amd64/vet -uns�� q62FCnbEi 64/pkg/tool/linu-I 64/pkg/tool/linux_amd64/vet g_.a amVETxCjQ ache/go/1.25.9/x--noprofile 64/pkg/tool/linux_amd64/vet (dns block)
  • nonexistent.local
    • Triggering command: /tmp/go-build3674478355/b509/launcher.test /tmp/go-build3674478355/b509/launcher.test -test.testlogfile=/tmp/go-build3674478355/b509/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true rotocol/go-sdk@v1.5.0/internal/json/json.go -I x_amd64/vet --gdwarf-5 --64 ut-4038412272.c x_amd64/vet -I g_.a -fPIC x_amd64/vet -pthread go-sdk/oauthex -fmessage-length-bool x_amd64/vet (dns block)
    • Triggering command: /tmp/go-build2111533505/b513/launcher.test /tmp/go-build2111533505/b513/launcher.test -test.testlogfile=/tmp/go-build2111533505/b513/testlog.txt -test.paniconexit0 -test.timeout=10m0s (dns block)
  • slow.example.com
    • Triggering command: /tmp/go-build3674478355/b509/launcher.test /tmp/go-build3674478355/b509/launcher.test -test.testlogfile=/tmp/go-build3674478355/b509/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true rotocol/go-sdk@v1.5.0/internal/json/json.go -I x_amd64/vet --gdwarf-5 --64 ut-4038412272.c x_amd64/vet -I g_.a -fPIC x_amd64/vet -pthread go-sdk/oauthex -fmessage-length-bool x_amd64/vet (dns block)
    • Triggering command: /tmp/go-build2111533505/b513/launcher.test /tmp/go-build2111533505/b513/launcher.test -test.testlogfile=/tmp/go-build2111533505/b513/testlog.txt -test.paniconexit0 -test.timeout=10m0s (dns block)
  • this-host-does-not-exist-12345.com
    • Triggering command: /tmp/go-build3674478355/b518/mcp.test /tmp/go-build3674478355/b518/mcp.test -test.testlogfile=/tmp/go-build3674478355/b518/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true olang.org/grpc@v1.80.0/internal/transport/bdp_estimator.go olang.org/grpc@v1.80.0/internal/transport/client_stream.go x_amd64/vet --gdwarf-5 g/grpc/metadata -o x_amd64/vet .cfg�� 2897549/b447/_pkg_.a -trimpath x_amd64/vet -p contextprotocol//usr/bin/runc -lang=go1.25 x_amd64/vet (dns block)
    • Triggering command: /tmp/go-build3099190320/b253/mcp.test /tmp/go-build3099190320/b253/mcp.test -test.testlogfile=/tmp/go-build3099190320/b253/testlog.txt -test.paniconexit0 -test.timeout=10m0s (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title [WIP] Fix duplicate code pattern in MCP text-content envelope construction Deduplicate raw MCP text envelope construction with shared helper Apr 22, 2026
Copilot AI requested a review from lpcox April 22, 2026 15:07
@lpcox lpcox marked this pull request as ready for review April 22, 2026 15:22
Copilot AI review requested due to automatic review settings April 22, 2026 15:22
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR centralizes construction of raw MCP “text” response envelopes to reduce duplication and drift risk across production call paths.

Changes:

  • Added mcp.BuildMCPTextResponse(text string) helper to build the canonical {content:[{type:"text",text:"..."}]} map shape.
  • Updated multiple server/proxy call sites to use the shared helper instead of inline map literals.
  • Added a focused unit test validating the helper’s envelope structure and text passthrough.
Show a summary per file
File Description
internal/mcp/tool_result.go Introduces the shared raw MCP text response envelope builder.
internal/mcp/tool_result_test.go Adds unit coverage for BuildMCPTextResponse.
internal/server/unified.go Replaces inline envelope construction with the shared helper for guard-facing permission calls.
internal/server/system_tools.go Uses the shared helper for system tool text responses (sysInit, listServers).
internal/proxy/proxy.go Uses the shared helper for proxy backend enrichment tool responses.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 5/5 changed files
  • Comments generated: 0

@lpcox lpcox merged commit bd075b0 into main Apr 22, 2026
30 checks passed
@lpcox lpcox deleted the copilot/fix-duplicate-mcp-text-envelope branch April 22, 2026 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[duplicate-code] Duplicate Code Pattern: Raw MCP Text-Content Envelope Construction

3 participants